package com.atguigu.cdc.handler;

import com.atguigu.cdc.model.AlbumCdcEntity;
import com.atguigu.cdc.model.CDCEntity;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.persistence.Column;

@Slf4j
@Component
@CanalTable("album_info")
public class AlbumCdcHandler implements EntryHandler<AlbumCdcEntity> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void update(AlbumCdcEntity before, AlbumCdcEntity after) {
        log.info("监听到album_info表修改数据，修改的id：{},专辑标题：{}",after.getId(),after.getAlbumTitle());
        String key = "album:info:" + after.getId();
        //删除redis的数据
        redisTemplate.delete(key);
        EntryHandler.super.update(before, after);
    }

    @Override
    public void delete(AlbumCdcEntity albumCdcEntity) {
        log.info("监听到album_info表修改数据，删除的id：{},专辑标题：{}",albumCdcEntity.getId(),albumCdcEntity.getAlbumTitle());
        String key = "album:info:" + albumCdcEntity.getId();
        //删除redis的数据
        redisTemplate.delete(key);
        EntryHandler.super.delete(albumCdcEntity);
    }
}
